package com.neusoft.supermarket.DAO.impl;

import com.neusoft.supermarket.DAO.UserInfoDAO;
import com.neusoft.supermarket.common.BaseDAO;
import com.neusoft.supermarket.entity.UserInfo_Entity;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class UserInfoDAOImpl extends BaseDAO<UserInfo_Entity> implements UserInfoDAO {


    public List<UserInfo_Entity> queryAll() {
        return null;
    }

    public int insert_UserInfo(UserInfo_Entity user) {
        return 0;
    }

    public int update_UserInfo(UserInfo_Entity user) {
        return 0;
    }

    public int delete_UserInfo(String userno) {
        return 0;
    }

    public UserInfo_Entity loginByUser(String account, String password) {
        String sql = "select * from userinfo where account=? and password=?";
        Object[] objects = {account, password};
        List<UserInfo_Entity> list = exec_Query(sql,objects,"用户登录!!!");

        if(list.size()>0)
        {
            return list.get(0);
        }
        return null;
    }

    public UserInfo_Entity queryUserByName(String name) {
        String sql = "select userno,uname,isadmin,phone from userinfo where uname=?";
        Object[] objects = {name};
        List<UserInfo_Entity> list = exec_Query(sql,objects,"查询成功!!!");

        if(list.size()>0)
        {
            return list.get(0);
        }
        return null;
    }


    protected List<UserInfo_Entity> resultToList(ResultSet rs) throws SQLException {
        List<UserInfo_Entity> list = new ArrayList<UserInfo_Entity>();
        while(rs.next())
        {
            UserInfo_Entity user = new UserInfo_Entity();
            if(isExistColumn(rs,"userno")) {
                user.setUserno(rs.getString("userno"));
            }
            if(isExistColumn(rs,"uname")) {
              user.setUname(rs.getString("uname"));
            }
            if(isExistColumn(rs,"phone")) {
                user.setPhone(rs.getString("phone"));
            }
            if(isExistColumn(rs,"account")) {
                user.setAccount(rs.getString("account"));
            }
            if(isExistColumn(rs,"password")) {
                user.setPassword(rs.getString("password"));
            }
            if(isExistColumn(rs,"isadmin")) {
                user.setIsadmin(rs.getString("isadmin"));
            }
            if(isExistColumn(rs,"isenable")) {
                user.setIsenable(rs.getString("isenable"));
            }
            if(isExistColumn(rs,"isonline")) {
                user.setIsonline(rs.getString("isonline"));
            }
            list.add(user);
        }
        return list;
    }
}
